package com.lavegue.common;

import java.util.Collection;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.lavegue.model.UserInfo;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class BaseAction extends ActionSupport {
	public static final String G_LOGIN_PAGE = "g_login";
	protected boolean isBlank(Collection list) {
		return CollectionUtils.isEmpty(list);
	}
	
	protected boolean isBlank(String str) {
		return StringUtils.isBlank(str);
	}
	
	protected void setRequestAtrribute(String key, Object value) {
		ActionContext.getContext().put(key, value);
	}
	
	protected Object getSessionAtrribute(String key) {
		Object object=null;
		ActionContext context = ServletActionContext.getContext();
		if(context != null){
			Map<String, Object> session = context.getSession();
			if(session != null){
				object = session.get(key);
			}
		}
		return object;
	}
	
	protected void setSessionAtrribute(String key, Object value) {
		ActionContext.getContext().getSession().put(key, value);
	}
	
	//获取会话 session 对象
	protected UserInfo getSessionUser() {
		return (UserInfo)getSessionAtrribute(Constants.SESSION_USER);
	}
	
	protected void removeSession(String remObj) {
//		Map<String, Object> session = ServletActionContext.getContext().getSession();
//		session.remove(remObj);
		ActionContext context = ServletActionContext.getContext();
		if(context != null){
			Map<String, Object> session = context.getSession();
			session.remove(remObj);//清除用户信息
		}
	}
	
	/**
	 * 获取工程绝对路径
	 * 如: E:\Tomcat\webapps\TEST\ 
	 * @return
	 */
	protected String getProjectPath() {
		return ServletActionContext.getServletContext().getRealPath("") + "/";
	}
}
